<template>
  <div
    :class="[hidden?'hidden':'',`position-${position}`]"
    class="pagination-container"
  >
    <el-pagination
      :background="background"
      :current-page="page"
      :page-size="limit"
      :layout="layout"
      :page-sizes="pageSizes"
      :total="total"
      size="small"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </div>
</template>

<script>
import {scrollTo} from '@/utils/scroll-to.js'

export default {
  name: 'Pagination',
  props: {
    position: {
      type: String,
      default: 'center',
    },
    total: {
      required: true,
      type: Number,
    },
    page: {
      type: Number,
      default: 1,
    },
    limit: {
      type: Number,
      default: 20,
    },
    pageSizes: {
      type: Array,
      default() {
        return [20, 50, 100]
      },
    },
    layout: {
      type: String,
      default: 'total, sizes, prev, pager, next, jumper',
    },
    background: {
      type: Boolean,
      default: true,
    },
    autoScroll: {
      type: Boolean,
      default: true,
    },
    hidden: {
      type: Boolean,
      default: false,
    },
  },
  setup(props, context) {
    // var pageSize = ref(props.limit)
    const handleSizeChange = (val) => {
      // pageSize.value = val
      context.emit('update:limit', val)
      context.emit('pagination')
      scrollTo(0, 800)
    }
    const handleCurrentChange = (val) => {
      context.emit('update:page', val)
      context.emit('pagination')
      scrollTo(0, 800)
    }

    return {
      // pageSize,
      handleSizeChange,
      handleCurrentChange,
    }
  },
}
</script>

<style scoped>
.pagination-container {
  background: #fff;
  padding: 20px 0px;
}

.position-left {
  display: flex;
  justify-content: flex-start;
  align-items: center;
}

.position-center {
  display: flex;
  justify-content: center;
  align-items: center;
}

.position-right {
  display: flex;
  justify-content: flex-end;
  align-items: center;
}

.pagination-container.hidden {
  display: none;
}
</style>
